/*
 * MeshMgr.cpp
 * Author: Johan Kristins
 *	
 */

#include "MeshMgr.h"

MeshMgr::MeshMgr()
{

};

MeshMgr::~MeshMgr()
{

};

bool MeshMgr::Init()
{
	m_xMeshMap.clear();

	return true;
};

void MeshMgr::Close()
{
	std::map<unsigned int, Mesh*>::iterator iter = m_xMeshMap.begin();
	for (; iter != m_xMeshMap.end(); iter++)
	{
		delete (*iter).second;
	}
	m_xMeshMap.clear();
};

Mesh* MeshMgr::LoadMesh(const char* filename)
{
	std::ifstream stream;
	stream.open(filename,  std::ifstream::binary);
	if (!stream.is_open())
	{
		return NULL;
	};

	int numVertices = 0;
	int numTexcoords = 0;
	int numNormals = 0;
	int numIndices = 0;

	Mesh* newMesh = new Mesh();

	//Read vertices
	stream.read((char*)&numVertices, sizeof(int));
	newMesh->vertex_array = new float[numVertices];
	stream.read((char*)newMesh->vertex_array, sizeof(float)*numVertices);

	//Read texcoords
	stream.read((char*)&numTexcoords, sizeof(int));
	newMesh->texcoord_array = new float[numTexcoords];
	stream.read((char*)newMesh->texcoord_array, (sizeof(float))*numTexcoords);

	//Read normals
	stream.read((char*)&numNormals, sizeof(int));
	newMesh->normal_array = new float[numNormals];
	stream.read((char*)newMesh->normal_array, (sizeof(float))*numNormals);

	//Read indices
	stream.read((char*)&numIndices, sizeof(int));
	newMesh->indice_array = new int[numIndices];
	stream.read((char*)newMesh->indice_array, (sizeof(int))*numIndices);

	newMesh->m_iNumIndices = numIndices;

	stream.close();
	//m_xMeshMap.insert(std::make_pair<unsigned int, Mesh*>(newMesh->m_ID, newMesh));
	return newMesh;
};